﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Resources;
using IPMS.AccessAuth;
using System.Threading;
using System.Globalization;


public partial class HeadingSection : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }
    protected void Sort(GridViewRowEventArgs e, GridView gv)
    {
        if ((e.Row != null) && (e.Row.RowType == DataControlRowType.Header))
        {
            foreach (TableCell cell in e.Row.Cells)
            {
                if (cell.HasControls())
                {
                    LinkButton button = (LinkButton)cell.Controls[0];
                    if (button != null)
                    {
                        Image image = new Image();
                        image.ImageUrl = "";
                        if (gv.SortExpression == button.CommandArgument)
                        {
                            if (gv.SortDirection == SortDirection.Ascending)
                            {
                                image.ImageUrl = "images/sortup.gif";
                            }
                            else
                            {
                                image.ImageUrl = "images/sortdown.gif";
                            }
                        }
                        if (image.ImageUrl != "")
                        {
                            cell.Controls.Add(image);
                        }
                    }
                }
            }
        }
    }


    private void showHeadingSections()
    {
        //DataTable dt = this.ODSHeadingSection.
    }

    private void clearText()
    {
        this.txtDescription.Text = "";
        this.txtSection.Text = "";
        hideErrorMsg();
    }

    protected void setTextReadOnly(bool bBool)
    {
        this.txtSection.ReadOnly = bBool;
        this.txtDescription.ReadOnly = bBool;
        hideErrorMsg();
    }

    protected void setButtons(string action)
    {
        switch (action)
        {
            case "modify":
                this.btnModify.Enabled = false;
                this.btnSave.Enabled = true;
                return;
        }
        this.btnModify.Enabled = this.txtSection.Text.Length != 0;
        this.btnSave.Enabled = false;
    }

    protected void hideErrorMsg()
    {
        this.lblError.Text = "";
        this.lblError.Visible = false;
    }

    protected void showErrorMsg(string msg)
    {
        this.lblError.Text = msg;
        this.lblError.Visible = true;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Auth canView = new Auth(this.Session["UserId"].ToString(), string.Empty, "ProcedureSection");
        if (!canView.CanView())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
        }
        if (!IsPostBack)
        {
            this.setButtons(string.Empty);
            this.setTextReadOnly(true);
            if (!this.Page.IsPostBack)
            {
                ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Settings + " | " + ISitemap.ProcedureSetup + " | " + ISitemap.HeadingSection;
            }
        }
    }

    protected void btnModify_Click(object sender, EventArgs e)
    {
        Auth canUpdate = new Auth(this.Session["UserId"].ToString(), string.Empty, "Countries");
        if (!canUpdate.CanUpdate())
        {
            showErrorMsg(Messages.mNoEdit.ToString());
        }
        else
        {
            this.setButtons("modify");
            this.setTextReadOnly(false);
            this.txtSection.ReadOnly = true;
            this.txtDescription.ReadOnly = false;
            this.txtDescription.Focus();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string trueUpdateStr = string.Empty;
        string falseUpdateStr = string.Empty;
        string trueInsertStr = string.Empty;
        string falseInsertStr = string.Empty;
        if (Session["Culture"] != null)
        {

            if (Session["Culture"].ToString() == "zh-TW")
            {
                trueUpdateStr = "更新成功!";
                falseUpdateStr = "更新失败!";
                trueInsertStr = "添加成功!";
                falseInsertStr = "添加失敗!";
            }
            else if (Session["Culture"].ToString() == "zh-CN")
            {
                trueUpdateStr = "更新成功!";
                falseUpdateStr = "更新失败!";
                trueInsertStr = "添加成功!";
                falseInsertStr = "添加失败!";
            }
            else
            {
                trueUpdateStr = "Update succeed!";
                falseUpdateStr = "Update failed!";
                trueInsertStr = "Add succeed!";
                falseUpdateStr = "Add failed!";
            }
        }
        try
        {
            this.ODSHeadingSection.UpdateMethod = "Update";
            this.ODSHeadingSection.UpdateParameters["Description"].DefaultValue = this.txtDescription.Text.ToString();
            this.ODSHeadingSection.UpdateParameters["OwnerGroupID"].DefaultValue = this.Session["OwnerGroupID"].ToString();
            this.ODSHeadingSection.UpdateParameters["Section"].DefaultValue = this.txtSection.Text.ToString();
            if (this.ODSHeadingSection.Update() < 0)
            {
                IPMS.Common.Jscript.Alert(trueUpdateStr, this.Page);
            }
            else
            {
                IPMS.Common.Jscript.Alert(falseUpdateStr, this.Page);
            }

            this.clearText();
            this.setTextReadOnly(true);
            this.setButtons(string.Empty);
        }
        catch (Exception ex)
        {
            this.lblError.Visible = true;
            showErrorMsg(ex.Message.ToString());
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        this.clearText();
        this.setTextReadOnly(true);
        this.setButtons(string.Empty);
    }
    protected void gvHeadingSection_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.txtSection.Text = this.gvHeadingSection.DataKeys[this.gvHeadingSection.SelectedIndex].Values["Section"].ToString();
        this.txtDescription.Text = this.gvHeadingSection.DataKeys[this.gvHeadingSection.SelectedIndex].Values["Description"].ToString();
        this.txtSection.Enabled = false;
        this.txtDescription.Enabled = true;
        this.txtDescription.Focus();
        this.setButtons(string.Empty);
    }
    protected void gvHeadingSection_RowCreated(object sender, GridViewRowEventArgs e)
    {
        this.Sort(e, this.gvHeadingSection);
    }
}
